package com.java.dao.impl;

import com.java.dao.GoodsDao;
import com.java.dao.OrderDao;
import com.java.domain.Goods;
import com.java.domain.Order;
import com.java.utils.DruidUtils;
import com.java.utils.JedisUtil;
import com.java.utils.SerializeUtil;
import org.apache.commons.dbutils.QueryRunner;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.List;

/**
 * @version 2020.3.4
 * @User 悠恋
 * @Data 2022/11/26
 * @Time 18:31
 */
public class OrderDaoImpl implements OrderDao {
    private QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    private GoodsDao gd = new GoodsDaoImpl();
    private JedisUtil jedisUtil = JedisUtil.getInstance();//实例化工具类
    private Jedis jedis = JedisUtil.getInstance().getJedis();//从中获取Jedis实例
    private JedisUtil.Strings strings = jedisUtil.new Strings();//从中获取Strings字符串类实例


    private String sql = "";

    @Override
    public int addOrder(Order order) {
        try {
            sql = "insert into `order`(oid,payment,status,createtime,uname) values(?,?,?,?,?)";
            Object[] params = {order.getOid(), order.getPayment(), order.getStatus(), order.getCreatetime(), order.getUname()};
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 修改商品的库存
    @Override
    public int updateInventory(int id, int num) {
        try {
            sql = "update goods set num = ? where gid = ?";
            int update = qr.update(sql, num, id);
            List<Goods> goodsList = gd.queryGoods();
            strings.set("MYSCGoodsCache", SerializeUtil.serialize(goodsList));//序列化好的数据取名为serializeUserinfo存储到redis
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
